jQuery(document).ready(function() {
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			$('#org').val(data.orgname);
			$('#org').attr('data', data.orgcode);
		}
	});
	initTable();
	App.setPage("flowSup");  //Set current page
	App.init(); //Initialise plugins and elements
	bindItem("itemtypecode", null, null);
	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	//绑定机构下拉列表
	bindOrg2('org');
	 
});


function initTable(){
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'supervise/limitList.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        queryParams:function(params) {
        	var param ={};
        	param.pageNum =  params.offset / params.limit + 1;
        	param.pageSize = params.limit;
        	param.orgCode = $('#org').attr('data');
        	var itemNo = $('#itemtypecode').val();
        	var startTime = $('#startapplytime').val();
        	var endTime = $('#endapplytime').val();
        	if(""!= itemNo)
        		param.itemNo = itemNo;
        	if(""!= startTime)
            	param.startApplyTime = startTime;
        	if(""!= endTime){
        		param.endApplyTime = endTime;
        	}
            return param;
        },
	    columns: [{
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '50px',
            formatter : function(value, row, index) {  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'applyno',
	        title: '申请单编号' ,
	        formatter:function(value,row,index){
	        	  return '<a onclick="showDetail(\''+ value +'\', \''+ row.instanceid +'\')">' + value + '</a>';  
	        }
	    }, {
	        field: 'itemname',
	        title: '政务项目' 
	    }, {
	        field: 'applytime',
	        title: '申请时间'
	    }, {
	        field: 'timelimit',
	        title: '设定时限(工作日)' 
	    }, {
	    	field: 'userdtime',
		    title: '已用时间(工作日)' 
	        
	    }, {
            field: 'orgname',
            title: '所属机构',
	    }, {
            field: 'nodename',
            title: '申请单状态',
            formatter : function(value, row, index) {
            	var rValue = value;
            	if(value == '办结')
            		rValue = '<span style="color:green;font-weight: bold;">'+ value +'</span>';
            	else if(value.indexOf('不予') >= 0)
            		rValue = '<span style="color:red;font-weight: bold;">'+ value +'</span>';
            	else
            		rValue = '<span style="color:blue;font-weight: bold;">'+ value +'</span>';

            	return rValue;  
		    } 
	    } , {
            field: 'status',
            title: '状态',
            formatter : function(value, row, index) {
            	if(row.userdtime - row.timelimit>0){
            		return '<span style="color:red;">过期</span>'; 
            	}else{
            		return '<span style="color:green;">正常</span>'; 
            	}
		    } 
	    }] 
	});
}

function showDetail(applyno, instanceid){
	if('' != instanceid){
		var param = {};
		param["instanceid"] = instanceid;
		
		$.ajax({ 
			method: 'post',
			url: projectPath + "business/getapproveusername.do",
			data: JSON.stringify(param),
			dataType: "json",
			contentType: "application/json",
			success: function(data){
				console.log(data)
				if(undefined != data.datas){
					username = '<span style="color: red">（当前人员：'+ data.datas +'）</span>';
					$('#modalTile').html("业务详细　-　" + applyno + username);
				}
			}
		});
	}
	setModalSize();
	
	$('#ifModal').attr("src", "../../query/detail.html?applyno=" + applyno+"&instanceid="+instanceid);
	
	$("#modal").modal('show');
}